package com.castle.fortress.admin.study.exam.rest;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.castle.fortress.admin.core.constants.GlobalConstants;
import com.castle.fortress.admin.study.exam.entity.StudyMemberExamEntity;
import com.castle.fortress.admin.study.exam.dto.StudyMemberExamDto;
import com.castle.fortress.admin.system.entity.SysUser;
import com.castle.fortress.admin.study.exam.service.StudyMemberExamService;
import com.castle.fortress.common.utils.ConvertUtil;
import com.castle.fortress.admin.utils.WebUtil;
import com.castle.fortress.common.entity.RespBody;
import com.castle.fortress.common.exception.BizException;
import com.castle.fortress.common.respcode.GlobalRespCode;
import com.castle.fortress.common.enums.OperationTypeEnum;
import com.castle.fortress.admin.core.annotation.CastleLog;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.logging.log4j.util.Strings;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 用户试卷 api 控制器
 *
 * @author castle
 * @since 2023-02-16
 */
@Api(tags="用户试卷api管理控制器")
@RestController
@RequestMapping("/api/study/exam/studyMemberExam")
public class ApiStudyMemberExamController {
    @Autowired
    private StudyMemberExamService studyMemberExamService;


    /**
     * 用户试卷的分页展示
     * @param studyMemberExamDto 用户试卷实体类
     * @param currentPage 当前页
     * @param size  每页记录数
     * @return
     */
    @CastleLog(operLocation="用户试卷-api",operType = OperationTypeEnum.QUERY)
    @ApiOperation("用户试卷分页展示")
    @GetMapping("/page")
    public RespBody<IPage<StudyMemberExamDto>> pageStudyMemberExam(StudyMemberExamDto studyMemberExamDto, @RequestParam(required = false) Integer currentPage, @RequestParam(required = false)Integer size){
        Integer pageIndex= currentPage==null? GlobalConstants.DEFAULT_PAGE_INDEX:currentPage;
        Integer pageSize= size==null? GlobalConstants.DEFAULT_PAGE_SIZE:size;
        Page<StudyMemberExamDto> page = new Page(pageIndex, pageSize);

        IPage<StudyMemberExamDto> pages = studyMemberExamService.pageStudyMemberExam(page, studyMemberExamDto);
        return RespBody.data(pages);
    }

    /**
     * 用户试卷保存
     * @param studyMemberExamDto 用户试卷实体类
     * @return
     */
    @CastleLog(operLocation="用户试卷-api",operType = OperationTypeEnum.INSERT)
    @ApiOperation("用户试卷保存")
    @PostMapping("/save")
    public RespBody<String> saveStudyMemberExam(@RequestBody StudyMemberExamDto studyMemberExamDto){
        if(studyMemberExamDto == null ){
            throw new BizException(GlobalRespCode.PARAM_MISSED);
        }
            StudyMemberExamEntity studyMemberExamEntity = ConvertUtil.transformObj(studyMemberExamDto,StudyMemberExamEntity.class);
        if(studyMemberExamService.save(studyMemberExamEntity)){
            return RespBody.data("保存成功");
        }else{
            return RespBody.fail(GlobalRespCode.OPERATE_ERROR);
        }
    }

    /**
     * 用户试卷编辑
     * @param studyMemberExamDto 用户试卷实体类
     * @return
     */
    @CastleLog(operLocation="用户试卷-api",operType = OperationTypeEnum.UPDATE)
    @ApiOperation("用户试卷编辑")
    @PostMapping("/edit")
    public RespBody<String> updateStudyMemberExam(@RequestBody StudyMemberExamDto studyMemberExamDto){
        if(studyMemberExamDto == null || studyMemberExamDto.getId() == null || studyMemberExamDto.getId().equals(0L)){
            throw new BizException(GlobalRespCode.PARAM_MISSED);
        }
            StudyMemberExamEntity studyMemberExamEntity = ConvertUtil.transformObj(studyMemberExamDto,StudyMemberExamEntity.class);
        if(studyMemberExamService.updateById(studyMemberExamEntity)){
            return RespBody.data("保存成功");
        }else{
            return RespBody.fail(GlobalRespCode.OPERATE_ERROR);
        }
    }

    /**
     * 用户试卷删除
     * @param ids 用户试卷id集合
     * @return
     */
    @CastleLog(operLocation="用户试卷-api",operType = OperationTypeEnum.DELETE)
    @ApiOperation("用户试卷删除")
    @PostMapping("/delete")
    public RespBody<String> deleteStudyMemberExam(@RequestBody List<Long> ids){
        if(ids == null || ids.isEmpty()){
            throw new BizException(GlobalRespCode.PARAM_MISSED);
        }
        if(studyMemberExamService.removeByIds(ids)) {
            return RespBody.data("保存成功");
        }else{
            return RespBody.fail(GlobalRespCode.OPERATE_ERROR);
        }
    }

    /**
     * 用户试卷详情
     * @param id 用户试卷id
     * @return
     */
    @CastleLog(operLocation="用户试卷-api",operType = OperationTypeEnum.QUERY)
    @ApiOperation("用户试卷详情")
    @GetMapping("/info")
    public RespBody<StudyMemberExamDto> infoStudyMemberExam(@RequestParam Long id){
        if(id == null){
            throw new BizException(GlobalRespCode.PARAM_MISSED);
        }
            StudyMemberExamEntity studyMemberExamEntity = studyMemberExamService.getById(id);
            StudyMemberExamDto studyMemberExamDto = ConvertUtil.transformObj(studyMemberExamEntity,StudyMemberExamDto.class);
        return RespBody.data(studyMemberExamDto);
    }


}
